#include "threadPool.h"
#include "head.h"
#include "serverFunc.h"
int puts_server(int netfd){
    char filename[1024] = {0};
    int length;
    recv(netfd,&length,sizeof(int),MSG_WAITALL);
    recv(netfd,filename,length,0);
    //   printf("file=%s",filename);
    //    filename[]="b/netdisk/file1";
    //  printf("strlen=%d\n",strlen(filename));
    int fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
    //   printf("%d\n",fd);
    char buf[1024] = {0};
    while(1){
        recv(netfd,&length,sizeof(int),MSG_WAITALL);
        if(length != 1000){
            printf("length = %d\n", length);
        }
        if(length == 0){
            break;
        }
        bzero(buf,sizeof(buf));
        recv(netfd,buf,length,MSG_WAITALL);
        write(fd,buf,length);
    }
    close(fd);
    return 0;
}

int makeWorker(threadPool_t *threadPool){
    for(int i = 0; i < threadPool->workerNum; ++i){
        pthread_create(&threadPool->tidArr[i],NULL,threadFunc,threadPool);
    }
    return 0;
}

void *threadFunc(void *arg){
    threadPool_t *threadPool = (threadPool_t *)arg;

    while(1){
        // 取出任务
        pthread_mutex_lock(&threadPool->taskQueue.mutex);
        while(threadPool->exitflag == 0 && threadPool->taskQueue.queueSize == 0){
            pthread_cond_wait(&threadPool->taskQueue.cond, &threadPool->taskQueue.mutex);
        }
        if(threadPool->exitflag != 0){
            printf("child is going to exit!\n");
            pthread_mutex_unlock(&threadPool->taskQueue.mutex);
            pthread_exit(NULL);
        }
        printf("child get a task!\n");
        int netfd = threadPool->taskQueue.pFront->netfd;
        deQueue(&threadPool->taskQueue);
        pthread_mutex_unlock(&threadPool->taskQueue.mutex);
        // 执行任务
        while(1)
        {
            int ret = checkLogin(netfd); 
            if(ret == 0)
            {
                break;
            }
        }

        char curWorkingDir[1024] = {0};
        char username[256] = {0};
        char mkdirPath[256] = {0};
        int length;
        recv(netfd,&length,sizeof(int),MSG_WAITALL);
        recv(netfd,username,length,MSG_WAITALL);
        int ret = mkdir(username,0777);
        perror("mkdir1");
        sprintf(mkdirPath,"%s/%s",username,"netdisk");
        ret = mkdir(mkdirPath,0777);
        perror("mkdir2");
        //构建一个栈，用于cd
        stack* st;
        st = initStack(st);

        if(ret == 0)
        {
            train_t train;
            train.length = strlen(mkdirPath);
            strcpy(train.data,mkdirPath);
            send(netfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);
        }         

        strcpy(curWorkingDir,mkdirPath);
        printf("curWorkingDir = %s\n",curWorkingDir);

        while(1)
        {
            int orderflag;
            char path[256] = {0};
            recvOrder(netfd,&orderflag,path);
            printf("first child path = %s\n",path);
            switch(orderflag)
            {
            case CD:
                printf("In cd curWorkingDir = %s\n",curWorkingDir);
                myCd(st,path,curWorkingDir);
                break;
            case LS:

                lsSend(netfd,curWorkingDir);
                break;

            case PUTS:

                puts_server(netfd);
                break;
            case PWD:
                pwdSend(netfd,curWorkingDir);
                break;
            case MKDIR:
                mkdircreat(curWorkingDir);
                break;
            case GETS:
                printf("server has passed the switch\n");
                gets_server(netfd,path,curWorkingDir);
                break;
            case GETS2:
                printf("gets_server has passed the switch\n");
                gets_server2(netfd,path,curWorkingDir);
                break;
            }
        }
        close(netfd);
        // 恢复空闲
        printf("child finish a task!\n");
    }
}

